//
//  SWTabBarLayout.m
//  SinaWeibo2
//
//  Created by zhuangqianwei on 16/11/28.
//  Copyright © 2016年 zhuangqianwei. All rights reserved.
//

#import "SWTabBarLayout.h"
@interface SWTabBarLayout ()
@property (nonatomic,weak)UIButton *plusButton;

@end

@implementation SWTabBarLayout

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *plusBtn = [[UIButton alloc] init];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];

        
        //撰写按钮的布局
        [plusBtn addTarget:self action:@selector(plusBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:plusBtn];
        self.plusButton = plusBtn;
    }
    return self;
}
//点击➕按钮的时候通知代理
- (void)plusBtnClick {
    if ([self.delegate respondsToSelector:@selector(btnDidClickPush:)]) {
        [self.delegate btnDidClickPush:self];
        NSLog(@"➕");
    }
    
}
- (void)layoutSubviews {
    
    [super layoutSubviews];
   
    CGFloat W = screenWidth / 5;
    CGFloat H = self.bounds.size.height;
    int index = 0;
//    self.plusButton.frame = CGRectMake(2 * W, H, W, H);
   //遍历tabbar
    for (UITabBar * subview in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([subview isKindOfClass:class]) {
            //设置 x 宽度 和 索引
            
            subview.frame = CGRectMake(index * W, 0, W, H);
            //下标递增
            index += 1;

            if (index == 2) {
                index += 1;
            }
        }
    }
    //撰写按钮的布局
    
    
    self.plusButton.center = CGPointMake(self.center.x, H * 0.5);
    self.plusButton.frame = CGRectMake(2*W, 0, W, H);
    

}

@end
